package org.ucas.kgraph.service;


import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;

import java.io.IOException;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Recorder;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.persistent.FileRecorder;

public class SourceService {
	//设置好账号的ACCESS_KEY和SECRET_KEY
	//public static Zone zone = Zone.zone1();
	//Zone zone=Zone.zone1();
	//Config.zone=Zone.zone1;
	/*Configuration config = new Configuration.Builder()
            .chunkSize(256 * 1024)  //分片上传时，每片的大小。 默认 256K
            .putThreshhold(512 * 1024)  // 启用分片上传阀值。默认 512K
            .connectTimeout(10) // 链接超时。默认 10秒
            .responseTimeout(60) // 服务器响应超时。默认 60秒
            .recorder(recorder)  // recorder 分片上传时，已上传片记录器。默认 null
            .recorder(recorder, keyGen)  // keyGen 分片上传时，生成标识符，用于片记录器区分是那个文件的上传记录
            .zone(Zone.zone0) // 设置区域，指定不同区域的上传域名、备用域名、备用IP。默认 Zone.zone0
            .build();
*/
	String ACCESS_KEY = "aPLyZj9CqD4ORVdEE7EcYxM0wmmQ6R6NNJFHEoy2";
	
	String SECRET_KEY = "_RjdiqmiqWNhCojqbQ38MHjSMXorDf6MS-n-y_JU";
	  //要上传的空间
	String bucketname = "zhangyipin";
	  //上传到七牛后保存的文件名
	String key = "my-java3.png";
	  //上传文件的路径
	//String filePath = "/.../...";

	String filePath = "C:/Users/yipin/Desktop/test/3.png";
	Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
	  //创建上传对象
	UploadManager uploadManager = new UploadManager();
	
	public String getUpToken(){
		return auth.uploadToken(bucketname,null,3600,new StringMap()
		          .put("callbackUrl","http://ode3u2kng.bkt.clouddn.com/callback")
		          .put("callbackBody", "filename=$(fname)&filesize=$(fsize)"));

	}

	public void upload() throws IOException{
	    try {
	      //调用put方法上传
	      Response res = uploadManager.put(filePath, key, getUpToken());

	      //打印返回的信息
	      System.out.println(res.bodyString()); 
	      } catch (QiniuException e) {
	          Response r = e.response;
	          // 请求失败时打印的异常的信息
	          System.out.println(r.toString());
	          try {
	              //响应的文本信息
	            System.out.println(r.bodyString());
	          } catch (QiniuException e1) {
	              //ignore
	          }
	      }       
	  }
	
	



	public static void main(String args[]) throws IOException{  
	    new SourceService().upload();
	  }

}